from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine.url import URL
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from config import MYSQL, DEBUG

Base = declarative_base()
mysql_conn_url = URL(
        drivername='mysql',
        username=MYSQL['username'],
        password=MYSQL['password'],
        host=MYSQL['host'],
        port=MYSQL['port'],
        database=MYSQL['database'],
        query={'charset': MYSQL['charset']}
    )

engine = create_engine(mysql_conn_url, encoding=MYSQL['charset'], echo=DEBUG)
Base.metadata.create_all(bind=engine)
db = scoped_session(sessionmaker(bind=engine))


